package com.yanqu.road.server.cmd.activity.pasture;

import com.yanqu.road.entity.rank.cross.CrossUserRank;
import com.yanqu.road.logic.cross.CrossActivityRankListModel;
import com.yanqu.road.message.YanQuMessage;
import com.yanqu.road.pb.activity.PastureProto;
import com.yanqu.road.server.cmd.CrossCommand;
import com.yanqu.road.server.command.Cmd;
import com.yanqu.road.server.logic.MessageHelper;
import com.yanqu.road.server.manager.activity.ActivityRankMgr;
import com.yanqu.road.server.protocol.GameProtocol;
import com.yanqu.road.server.protocol.Protocol;
import com.yanqu.road.utils.YanQuMessageUtils;

import java.math.BigInteger;
import java.util.LinkedList;

@Cmd(code = Protocol.C2_PASTURE_ACTIVITY_GAME_OVER, desc = "")
public class CrossPastureGameOverCmd extends CrossCommand {

    private int activityId;
    private int conditionType;
    private long userId;
    private long score;
    private boolean isPractice;
    LinkedList<CrossUserRank> otherRankList;

    @Override
    public boolean parseData(YanQuMessage packet) throws Exception {
        PastureProto.CrossPastureGetRankReqMsg reqMsg = PastureProto.CrossPastureGetRankReqMsg.parseFrom(packet.getBytes());
        userId = packet.getPlayerId();
        activityId = reqMsg.getActivityId();
        conditionType = reqMsg.getConditionType();
        score = reqMsg.getScore();
        isPractice = reqMsg.getIsPractice();
        return true;
    }

    @Override
    public void execute(YanQuMessage packet) throws Exception {
        ActivityRankMgr.changeActivityNewRank(activityId, conditionType, null);
        CrossActivityRankListModel rankListModel = ActivityRankMgr.getCrossActivityNewRankListModel(activityId, conditionType);
        int newRank = -1;
        if (rankListModel != null) {
            otherRankList = rankListModel.getRankList();
            CrossUserRank otherUserRank = new CrossUserRank();
            otherUserRank.setType(activityId);
            otherUserRank.setUserId(userId);
            otherUserRank.setBigRankType(conditionType);
            otherUserRank.setValue(BigInteger.valueOf(score));
            otherUserRank.setParam("");
            rankListModel.otherRankChange(otherUserRank, otherRankList);
            newRank = rankListModel.getOtherRank(userId, serverId, otherRankList);
        }
        PastureProto.CrossPastureGetRankRespMsg.Builder msg = PastureProto.CrossPastureGetRankRespMsg.newBuilder();
        msg.setActivityId(activityId);
        msg.setRank(newRank);
        msg.setIsPractice(isPractice);
        YanQuMessage message = YanQuMessageUtils.buildMessage(GameProtocol.S_CROSS_PASTURE_GAME_OVER, msg);
        MessageHelper.sendPacket(serverId, userId, message);
    }

    @Override
    public void sendData() {

    }
}
